<?php
class AttrController extends NodeController {
	
	/**
	 * 自定义标签
	 * Enter description here .
	 */
	public function flag() {
		if ($_POST) {
			$name = trim ( $_POST ['name'] );
			FlagModel::instance ()->addFlag ( array ('name' => $name ) );
			$this->redirect ( SUCCESS, '/news/flag' );
			exit ();
		}
		$this->view ()->assign ( 'cachetype', 0 );
		$flag = FlagModel::instance ()->getFlag ();
		$this->view ()->assign ( 'flag', $flag );
		$this->view ()->clearCache ( 'file:news/flag.tpl' );
		$this->view ()->display ( 'file:news/flag.tpl' );
	}
	
	/**
	 * 多值字段
	 */
	public function moreattr() {
		$rs = ExtendModel::instance ()->getExtend ( array ('status' => 3 ) );
		$this->view ()->assign ( "cachetype", 1 );
		$this->view ()->assign ( "attr", $rs );
		$this->view ()->clearCache ( "file:news/moreattr.tpl" );
		$this->view ()->display ( "file:news/moreattr.tpl" );
	}
	/**
	 * 自定义字段
	 * Enter description here .
	 */
	public function attr() {
		if ($_POST) {
			$key = trim ( strtolower ( $_POST ['key'] ) ); //强制转化为小写
			$name = trim ( $_POST ['name'] );
			$flag = ExtendModel::instance ()->getExtend ( array ('key' => $key ) );
			if (count ( $flag ) > 0) {
				$this->sendNotice ( 'key already exsit', null, false );
			}
			$params = array ('key' => trim ( $_POST ['key'] ), 'name' => trim ( $_POST ['name'] ), 'type' => trim ( $_POST ['attribute'] ), 'status' => $_POST ['status'], 'moduleid' => $_POST ['module'] );
			ExtendModel::instance ()->addExtend ( $params );
			$this->sendNotice ( SUCCESS, null, true );
		}
		$this->view ()->assign ( 'cachetype', 0 );
		$extend = ExtendModel::instance ()->getExtend ();
		$this->view ()->assign ( 'extend', $extend );
		$this->view ()->clearCache ( 'file:news/attr.tpl' );
		$this->view ()->display ( "file:news/attr.tpl" );
	}
	
	/**
	 * 分类绑定字段
	 */
	public function bindComment() {
		$config = require_once ROOT . '/config/base.php';
		$s = include ROOT . '/config/smarty.php';
		
		$this->view ()->assign ( 'cachetype', 1 );
		$this->view ()->assign ( 'config', $config );
		$categoryid = isset ( $_GET ['cid'] ) ? $_GET ['cid'] : 0;
		if ($this->view ()->isCached ( 'file:news/cate.extend.tpl', $categoryid )) {
			$this->view ()->assign ( 'cachetype', 1 );
			$this->view ()->display ( 'file:news/cate.extend.tpl', $categoryid );
			exit ();
		}
		$this->view ()->assign ( 'cachetype', 0 );
		$category = $this->getCategory ( $categoryid );
		$extend = NewsModel::instance ()->getExtend ( array ('module' => 'comment' ) );
		$cateInfo = NewsModel::instance ()->getCateExtendByCid ( $categoryid );
		$selected = array ();
		foreach ( $cateInfo as $v ) {
			array_push ( $selected, $v ['eid'] );
		}
		foreach ( $extend as $k => $v ) {
			if (in_array ( $v ['eid'], $selected )) {
				$extend [$k] ['checked'] = 'checked';
			}
		}
		$this->view ()->assign ( 'category', $category );
		$this->view ()->assign ( 'extend', $extend );
		$this->view ()->display ( 'file:news/comment.extend.tpl', $categoryid );
	}
}